//
//  theaterViewController.m
//  timemovie
//
//  Created by Yiqiao on 15/7/17.
//  Copyright (c) 2015年 Yiqiao. All rights reserved.
//

#import "theaterViewController.h"
#import "theatreCell.h"
#import "theatreModel.h"
@interface theaterViewController (){
    BOOL flag[100];
    NSArray *_districtArray;
}

@end

@implementation theaterViewController

- (void)_loadData{
    
    NSString *filePath1 = [[NSBundle mainBundle]pathForResource:@"district_list.json" ofType:nil];
    NSData *data1 = [NSData dataWithContentsOfFile:filePath1];
    NSDictionary *dictionary1 =[NSJSONSerialization JSONObjectWithData:data1 options:NSJSONReadingMutableContainers error:nil];
    _districtArray = [dictionary1 objectForKey:@"districtList"];
   
    NSString *filePath2 = [[NSBundle mainBundle]pathForResource:@"cinema_list.json" ofType:nil];
    NSData *data2 = [NSData dataWithContentsOfFile:filePath2];
    NSDictionary *dictionary2 =[NSJSONSerialization JSONObjectWithData:data2 options:NSJSONReadingMutableContainers error:nil];
    
    NSArray *cinemaList = [dictionary2 objectForKey:@"cinemaList"];
    _theatreDataArray= [[NSMutableArray alloc]init];
    
    for (NSDictionary *dictionary in cinemaList) {
        theatreModel *model = [[theatreModel alloc]init];
        model.name = [dictionary objectForKey:@"name"];
    //    model.address = [dictionary objectForKey:@"address"];
        model.districtId = [dictionary objectForKey:@"districtId"];
        [_theatreDataArray addObject:model ];
    }
    
    for (NSMutableDictionary *dictionary in _districtArray) {
        NSMutableArray *newCinema = [[NSMutableArray alloc ]init];
        [dictionary setObject:newCinema forKey:@"cinema"];
    
        NSString *areaId = [dictionary objectForKey:@"id"];
        for (theatreModel *model in _theatreDataArray) {
            if ([model.districtId isEqualToString: areaId]) {
                [newCinema addObject:model];
               
                
            //    NSLog(@"%@",model.name);
            
                
            }
        }
    }
}

- (void)_createTheatreTable{
    _theatreTable = [[UITableView alloc]initWithFrame:self.view.bounds];
    _theatreTable.dataSource = self;
    _theatreTable.delegate = self;
    _theatreTable.backgroundColor = [UIColor clearColor];
    [self.view addSubview:_theatreTable];
    [_theatreTable registerClass:[theatreCell class] forCellReuseIdentifier:@"cell"];
}

- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForHeaderInSection:(NSInteger)section{
    return 40;
}

- (void)buttonAction:(UIButton *)button{
    NSInteger index = button.tag;
    flag[index] = !flag[index];
    NSIndexSet *indexSet = [NSIndexSet indexSetWithIndex:index];
    [_theatreTable reloadSections:indexSet withRowAnimation:UITableViewRowAnimationFade];
}

- (UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIButton *button = [[UIButton alloc]init];
    NSDictionary *dictionary = _districtArray[section];
    NSString *areaName = [dictionary objectForKey:@"name"];
    [button setTitle:areaName forState:UIControlStateNormal];
    [button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    button.tag = section;
    return button;
}

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    theatreCell *cell = [[theatreCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
    NSDictionary *dictionary = _districtArray[indexPath.section];
    NSArray *cinema = [dictionary objectForKey:@"cinema"];
    theatreModel *model = cinema[indexPath.row];
   // cell.model = model;
    NSLog(@"%@",model.name);
    
    cell.textLabel.text = model.name;
    
    return cell;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    NSDictionary *dictionary = _districtArray[section];
    NSArray *number = [dictionary objectForKey:@"cinema"];
    if (flag[section]==NO) {
        return 0;
    }
    return number.count;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return _districtArray.count;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self _createTheatreTable];
    [self _loadData];
    self.title = @"Theatre";
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
